package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.google.android.material.tabs.TabLayout;
import com.lenovo.manufacture.Adapter.recAdapter;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.net.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class module10 extends BaseActivity {

    @BindView(R.id.m10tl)
    TabLayout m10tl;
    @BindView(R.id.m10vp)
    ViewPager m10vp;

    String[] title = {"车辆定制", "订单详情"};
    List<View> views = new ArrayList<>();
    List<Map<String, String>> appointments;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    int carId, engine, num, speed, control, brake, hang, wheel;
    long time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module10);
        ButterKnife.bind(this);
        init();
        initlsit();
    }

    private void initlsit() {
        async(data.getAppointment(), v -> {
            String engine = "Volvo", speed, control = "低配", brake, hang = "X", wheel;
            appointments = v.getData();
            List<String> list = new ArrayList<>();
            for (Map<String, String> appointment : appointments) {
                list.add(appointment.get("id"));
                list.add(simpleDateFormat.format(new Date(Long.valueOf(appointment.get("time")) * 1000)));
                list.add(appointment.get("num"));
                switch (appointment.get("engine")) {
                    case "1":
                        engine = "Saab";
                        break;
                    case "2":
                        engine = "Fiat";
                        break;
                    case "3":
                        engine = "Audi";
                        break;
                }

                list.add(engine);
                list.add(appointment.get("speed").equals("0") ? "自动" : "手动");
                list.add(appointment.get("wheel").equals("0") ? "烤漆" : "电镀");
                switch (appointment.get("control")) {
                    case "1":
                        control = "中配";
                        break;
                    case "2":
                        control = "高配";
                        break;
                }
                list.add(control);
                list.add(appointment.get("brake").equals("0") ? "鼓式制动" : "盘式制动");
                switch (appointment.get("hang")) {
                    case "1":
                        hang = "J";
                        break;
                    case "2":
                        hang = "Z";
                        break;
                    case "3":
                        hang = "A";
                        break;
                    case "4":
                        hang = "E";
                        break;
                    case "5":
                        hang = "XH";
                        break;
                    case "6":
                        hang = "F";
                        break;
                }
                list.add(hang);
            }
            RecyclerView recyclerView = (RecyclerView) views.get(1).findViewById(R.id.m10rec);
            recyclerView.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerView.setAdapter(new recAdapter(list));
        });

    }

    private void init() {
        View view1 = LayoutInflater.from(this).inflate(R.layout.activity_module10_1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.activity_module10_2, null);

        view1.findViewById(R.id.m10but1).setOnClickListener(view -> new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @SuppressLint("NewApi")
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(i, i1, i2);
                time = calendar.getTimeInMillis() / 1000;
                ((BootstrapButton) view).setBootstrapText(new BootstrapText.Builder(module10.this).addText(String.format("%d/%d/%d",i,i1,i2)).build());
            }
        }, 2020, 11, 12).show());

        //提交
        ((Button) view1.findViewById(R.id.m10but2)).setOnClickListener(view -> {
            switch (((RadioGroup) view1.findViewById(R.id.m10_carType)).getCheckedRadioButtonId()) {
                case R.id.m10_carType_1:
                    carId = 0;
                    break;
                case R.id.m10_carType_2:
                    carId = 1;
                    break;
                case R.id.m10_carType_3:
                    carId = 2;
                    break;
            }
            speed = ((RadioGroup) view1.findViewById(R.id.m10_1)).getCheckedRadioButtonId() == R.id.m10_1_1 ? 0 : 1;
            wheel = ((RadioGroup) view1.findViewById(R.id.m10_2)).getCheckedRadioButtonId() == R.id.m10_2_1 ? 0 : 1;
            switch (((RadioGroup) view1.findViewById(R.id.m10_3)).getCheckedRadioButtonId()) {
                case R.id.m10_3_1:
                    control = 0;
                    break;
                case R.id.m10_3_2:
                    control = 1;
                    break;
                case R.id.m10_3_3:
                    control = 2;
                    break;
            }
            brake = ((RadioGroup) view1.findViewById(R.id.m10_4)).getCheckedRadioButtonId() == R.id.m10_4_1 ? 0 : 1;
            num = Integer.valueOf(((EditText) view1.findViewById(R.id.m10et)).getText().toString());
            engine = ((Spinner) view1.findViewById(R.id.m10sp1)).getSelectedItemPosition();
            hang = ((Spinner) view1.findViewById(R.id.m10sp2)).getSelectedItemPosition();
            json.put("carId", carId);
            json.put("time", time);
            json.put("engine", engine);
            json.put("num", num);
            json.put("speed", speed);
            json.put("control", control);
            json.put("brake", brake);
            json.put("hang", hang);
            json.put("wheel", wheel);
            if (carId != 0 && time != 0 && speed != 0 && control != 0 && brake != 0 && wheel != 0)
                async(data.UserAppointment(json), v -> {
                    showMsg("下单成功!");
                    initlsit();
                });
            else showMsg("请完善订单!");
        });

        views.add(view1);
        views.add(view2);
        m10vp.setAdapter(new adapter(views));
        m10tl.setupWithViewPager(m10vp);
    }

    class adapter extends PagerAdapter {
        List<View> views;

        public adapter(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}